import java.net.InetAddress;


public class Source {
	private ISocket _socket = null;			// active socket
	private InetAddress _IP = null;			// active IP
	private int _port;						// active port
	
	/**
	 * Constructor
	 * @param socket
	 * @param ip
	 * @param port
	 */
	public Source(ISocket socket, InetAddress ip, int port){
		_socket = socket;
		_IP = ip;
		_port = port;
	}

	/**
	 * Constructor
	 * @param ip
	 * @param port
	 */
	public Source(InetAddress ip, int port){
		_IP = ip;
		_port = port;
		_socket = null;
	}
	
	// get functions //
	public ISocket getSocket() { return _socket; }
	
	public InetAddress getIP() { return _IP; }
	
	public int getPort() { return _port; }
	
	/**
	 * equals function
	 */
	@Override
	public boolean equals(Object o){
		if (! (o instanceof Source))
			return false;
		
		Source s = (Source) o;
		
		if(_socket == null || s._socket == null){
			if (_IP.equals(s._IP) && _port == s._port)
				return true;
		} 
		else{ 	
			if (_socket.type().equals(s._socket.type()) &&
					_IP.equals(s._IP) &&
					_port == s._port)
				return true;
		}
		return false;
	}
	
	/**
	 * hashCode function
	 */
	@Override
	public int hashCode(){
		if (_socket == null)
			return _IP.hashCode() * 17 + new Integer(_port).hashCode();
		
		return (_socket.hashCode()*31 + _IP.hashCode()) * 17 + new Integer(_port).hashCode();
	}
}
